﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using SkypeQuoteEditor.Properties;

namespace SkypeQuoteEditor
{
    public partial class SkypeQuoteEditor : Form
    {
        public SkypeQuoteEditor()
        {
            InitializeComponent();
            Icon = Resources.skype_metal;
        }

        private const string DataKey = "SkypeMessageFragment";
        private readonly Dictionary<string, object> _clipboadData = new Dictionary<string, object>();
        private string _message;
        private AboutBox _ab;

        private void LoadClipboard()
        {
            var iData = Clipboard.GetDataObject();
            if (iData != null)
                foreach (var format in iData.GetFormats())
                {
                    _clipboadData[format] = iData.GetData(format);
                }

            if (_clipboadData.ContainsKey(DataKey) && _clipboadData[DataKey] != null)
                using (var streamReader = new StreamReader(_clipboadData[DataKey] as MemoryStream))
                {
                    _message = streamReader.ReadToEnd();
                    var memoryStream = _clipboadData[DataKey] as MemoryStream;
                    if (memoryStream != null)
                        memoryStream.Seek(0, SeekOrigin.Begin);
                }

            richTextBoxQuote.Text = _message;
        }

        private void SaveClipboard()
        {
            _message = richTextBoxQuote.Text;
            var memoryStream = new MemoryStream();
            var streamWriter = new StreamWriter(memoryStream);

            streamWriter.Write(_message);
            streamWriter.Flush();
            memoryStream.Seek(0, SeekOrigin.Begin);
            _clipboadData[DataKey] = memoryStream;

            IDataObject iData = new DataObject();

            foreach (var item in _clipboadData)
            {
                iData.SetData(item.Key, item.Value);
            }

            Clipboard.SetDataObject(iData, true);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveClipboard();
        }

        private void loadFromClipboardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadClipboard();
        }

        private void howToUseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (_ab == null || _ab.IsDisposed)
                _ab = new AboutBox();
            _ab.Show();
        }

        private void getLastVersionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Process.Start(Resources.LastVersionLink);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Resources.AboutBox_link_LinkClicked_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
